<template>
  <div class="user-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户管理</span>
          <el-button type="primary" size="small" @click="handleAddUser">新增用户</el-button>
        </div>
      </template>
      <div class="content-body">
        <!-- 搜索和筛选区域 -->
        <div class="search-filter">
          <el-input 
            v-model="searchQuery" 
            placeholder="请输入用户名搜索"
            style="width: 200px; margin-right: 10px;"
            prefix-icon="Search"
            clearable
          />
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </div>
        
        <!-- 用户列表 -->
        <el-table :data="users" style="width: 100%; margin-top: 20px;">
          <el-table-column prop="id" label="用户ID" width="80" />
          <el-table-column prop="userName" label="用户名" width="120" />
          <el-table-column prop="role" label="角色" width="100" />
          <el-table-column prop="createTime" label="创建时间" width="180" />
          <el-table-column prop="status" label="状态" width="80">
            <template #default="scope">
              <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">
                {{ scope.row.status === 'active' ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template #default="scope">
              <el-button type="primary" size="small" @click="handleEditUser(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="handleDeleteUser(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <!-- 分页 -->
        <div class="pagination" style="margin-top: 20px; text-align: right;">
          <el-pagination 
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Search } from '@element-plus/icons-vue'

// 搜索条件
const searchQuery = ref('')

// 表格数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 模拟用户数据
const users = ref([
  { id: 1, userName: 'admin', role: '管理员', createTime: '2023-01-01 10:00:00', status: 'active' },
  { id: 2, userName: 'user1', role: '普通用户', createTime: '2023-01-02 14:30:00', status: 'active' },
  { id: 3, userName: 'user2', role: '普通用户', createTime: '2023-01-03 09:15:00', status: 'disabled' },
  { id: 4, userName: 'user3', role: '普通用户', createTime: '2023-01-04 16:45:00', status: 'active' },
  { id: 5, userName: 'user4', role: '普通用户', createTime: '2023-01-05 11:20:00', status: 'active' }
])

// 搜索方法
const handleSearch = () => {
  ElMessage.info(`搜索用户: ${searchQuery.value}`)
  // 实际项目中这里应该调用API进行搜索
}

// 新增用户
const handleAddUser = () => {
  ElMessage.info('打开新增用户弹窗')
  // 实际项目中这里应该打开新增用户的弹窗
}

// 编辑用户
const handleEditUser = (user) => {
  ElMessage.info(`编辑用户: ${user.userName}`)
  // 实际项目中这里应该打开编辑用户的弹窗，并填充用户数据
}

// 删除用户
const handleDeleteUser = (userId) => {
  ElMessage.warning(`删除用户: ${userId}`)
  // 实际项目中这里应该弹出确认对话框，确认后调用API删除用户
}

// 分页相关方法
const handleSizeChange = (size) => {
  pageSize.value = size
  ElMessage.info(`每页显示 ${size} 条数据`)
}

const handleCurrentChange = (current) => {
  currentPage.value = current
  ElMessage.info(`当前页码: ${current}`)
}
</script>

<style scoped>
.user-management {
  padding: 20px;
}

.content-body {
  padding-top: 10px;
}

.search-filter {
  display: flex;
  align-items: center;
}
</style>